<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Helper\Socket;
use DrSlump\Protobuf;
use App\Http\Helper\HelpFunc;

class CmdController extends Controller
{
    function index()
    {
        echo 'test cmd';
    }

    function testcmd(){
        $hf = new HelpFunc();
        $obj = $hf->StructNum(541);
        var_dump($obj);
    }

    function cmdLogin($test)
    {
        Protobuf::autoload();
        $helpf = new HelpFunc();

        $req = $helpf->StructNum(541);
        $req->setTest($test);

        $second_data = $req->serialize();

        $second_len = pack("I*",strlen($second_data) + 2);
        $second_num = substr(pack("I*",541),0,2);

        $second_pack = $second_len.$second_num.$second_data;

        $port = 7001;
        $ip = "192.168.128.198";

        // $result = $helpf->socket($ip,$port,$second_pack);
        $socket = Socket::singleton();
        $conn = $socket->connect($ip,$port);

        $sockRequest = $socket->sendRequest($second_pack);
        /* echo "request \r\n";
         var_dump($sockRequest);*/
        $getrepost = $socket->getResponse();

        $unPackDatas = $helpf->unPackData($getrepost);
        /* echo "respose \r\n";
         var_dump($unPackDatas);*/
        $Ans = $helpf->StructNum($unPackDatas['num'][1]);
        $Ans->parse($unPackDatas['data']);
        $result = $Ans->getTest();

        $str = (string)$result;
        echo "==============\r\n";
        var_dump($result);
    }
}
